import request from '@/utils/request';

// 发布内容相关接口类型定义
export interface Publish {
  publish_id?: number;
  content: string;
  user_id?: number;
  is_public?: boolean;
  like_count?: number;
  comment_count?: number;
  repost_count?: number;
  repost_origin_id?: number;
  created_at?: string;
  author?: {
    user_id: number;
    name: string;
    avatar?: string;
  };
  images?: Array<{
    upload_id: number;
    fileurl: string;
  }>;
  is_liked?: boolean;
  comments?: Comment[];
}

export interface Comment {
  comment_id?: number;
  content: string;
  user_id?: number;
  publish_id?: number;
  reply_to_comment_id?: number;
  created_at?: string;
  author?: {
    user_id: number;
    name: string;
    avatar?: string;
  };
  replyTo?: {
    comment_id?: number;
    author?: {
      user_id: number;
      name: string;
      avatar?: string;
    };
  };
  replies?: Comment[];
}

export interface Like {
  like_id?: number;
  user_id?: number;
  publish_id?: number;
  created_at?: string;
  user?: {
    user_id: number;
    name: string;
    avatar?: string;
  };
}

// 发布内容相关接口
export const createPublish = (data: { content: string; is_public?: boolean; image_ids?: number[] }) => {
  return request.post<Publish>('/publish/create', data);
};

export const getPublishList = (data: { page?: number; limit?: number; keyword?: string; sort_field?: string; sort_order?: string, source?: string }) => {
  // 设置默认值
  const params = { source: 'management_system', ...data };
  return request.post<{ list: Publish[]; pagination: any }>('/publish/list', params);
};

export const getPublishListAll = (data: { keyword?: string; sort_field?: string; sort_order?: string, source?: string }) => {
  // 设置默认值
  const params = { source: 'management_system', ...data };
  return request.post<Publish[]>('/publish/listAll', params);
};

export const getPublishById = (data: { publish_id: number }) => {
  return request.post<Publish>('/publish/detail', data);
};

export const deletePublish = (data: { publish_id: number }) => {
  return request.post('/publish/delete', data);
};


// 批量删除发布内容
export const batchDeletePublish = (data: { publish_ids: number[] }) => {
  return request.post('/publish/batchDelete', data);
};

export const editPublish = (data: { publish_id: number; content: string; is_public?: boolean; image_ids?: number[] }) => {
  return request.post<Publish>('/publish/edit', data);
};

// 评论相关接口
export const createComment = (data: { publish_id: number; content: string; reply_to_comment_id?: number }) => {
  return request.post<Comment>('/publish/comment/create', data);
};

export const deleteComment = (data: { comment_id: number }) => {
  return request.post('/publish/comment/delete', data);
};

export const getCommentList = (data: { publish_id: number; page?: number; limit?: number }) => {
  return request.post<{ list: Comment[]; pagination: any }>('/publish/comment/list', data);
};

// 点赞相关接口
export const createLike = (data: { publish_id: number }) => {
  return request.post<Like>('/publish/like/create', data);
};

export const deleteLike = (data: { publish_id: number }) => {
  return request.post('/publish/like/delete', data);
};

export const getLikeList = (data: { publish_id: number; page?: number; limit?: number }) => {
  return request.post<{ list: Like[]; pagination: any }>('/publish/like/list', data);
};